#!/bin/sh
function get_host_arch(){
  local IS_Linux=$(uname -a | grep "Linux")
  local IS_X86_64=$(uname -a | grep "X86_64")
  user=`whoami`
  if [ "$IS_Linux" ]; then
    HOST_ARCH=linux
    PREFIX="/home/${user}"
  elif [ "$IS_X86_64" ]; then
    HOST_ARCH=mac-x64
    PREFIX="/Users/${user}"
  else
    HOST_ARCH=mac-arm64
    PREFIX="/Users/${user}"
  fi
}

function get_branch_config(){
  BRANCH_CONFIG=`echo $VERSION_CONFIG | jq -r ".[0].configs"`
}

function list_config(){
  cd ${BRANCH_DIR}
  DATE=$(date '+%Y%m%d')
  LOG_DIR=$SCRIPT_DIR/build_log/$(date '+%Y%m%d')
  STR_TIME=$(date '+%H%M')

  if [ ! -d $LOG_DIR ]; then
      mkdir -p $LOG_DIR
  fi

  echo building %$BRANCH_DIR OHOS_NDK_HOME=$OHOS_NDK_HOME
  BRANCH_NAME=`echo $BRANCH_DATA | jq -r '.[0].branch_name'`
  echo "Update code in $BRANCH_DIR/src/flutter"
  # ./ohos -b $BRANCH_NAME 2>&1 | tee $LOG_DIR/${STR_TIME}_${BRANCH_NAME}_git.log
  # if [ $? != 0 ];then
  #   echo "./ohos -b $BRANCH_NAME failed."
  #   exit 1
  # fi
  rm -rf $(find $BRANCH_DIR/src/out -name flutter.har)

  CONFIG_NUM=`echo $BRANCH_CONFIG | jq -r '.[0].config_num'`
  for ((CONFIG_INDEX=1; CONFIG_INDEX<=CONFIG_NUM; CONFIG_INDEX++))
  do
    CONFIG_DATA=`echo $BRANCH_CONFIG | jq -r ".[0].config${CONFIG_INDEX}"`
    CONFIG_PATCH=""
    CONFIG_PATCH=`echo $CONFIG_DATA | jq -r ".[0].patch"`
    cd ${BRANCH_DIR}/src/flutter
    git stash
    if [ "${CONFIG_PATCH}" != "" ] && [ "${CONFIG_PATCH}" != "null" ]; then
      git apply "$SCRIPT_DIR/arch/$HOST_ARCH/patch/${CONFIG_PATCH}"
    fi
    cd ${BRANCH_DIR}
    
    CONFIG_CMD=`echo $CONFIG_DATA | jq -r '.[0].cmd'`
    CONFIG_IGONRE_ERROR=`echo $CONFIG_DATA | jq -r ".[0].ignore_error"`
    echo "build cmd is $CONFIG_CMD"
    pwd
    $CONFIG_CMD 2>&1 | tee $LOG_DIR/${STR_TIME}_${BRANCH_NAME}_config${CONFIG_INDEX}.log
    if [ $? != 0 ] && [ $CONFIG_IGONRE_ERROR == "null" ]; then
      echo "build $BRANCH_NAME config $CONFIG_INDEX failed"
      exit 1
    fi
  done
}

function list_branch(){
  VERSION_NAME=`echo $VERSION_CONFIG | jq -r ".[0].version_name"`
  if [[ "${BUILD_FLUTTER_VERSION[@]}"  =~ "${VERSION_NAME}" ]]; then
    get_branch_config
    BRANCHES=`echo $VERSION_CONFIG | jq -r ".[0].branches"`
    BRANCH_NUM=`echo $BRANCHES | jq -r ".[0].branch_number"`
    for ((BRANCH_INDEX=1; BRANCH_INDEX<=BRANCH_NUM; BRANCH_INDEX++))
    do
      BRANCH_DATA=`echo $BRANCHES | jq -r ".[0].branch${BRANCH_INDEX}"`
      BRANCH_DIR=`echo $BRANCH_DATA | jq -r '.[0].branch_dir'`
      BRANCH_DIR=${PREFIX}/${BRANCH_DIR}
      echo branch dir is $BRANCH_DIR
      if [ ! -d "${BRANCH_DIR}" ] || [ ! -d "${BRANCH_DIR}/src/flutter" ]; then
        echo ${BRANCH_DIR}/src/flutter not exist!
        exit 1
      fi
      BRANCH_API=`echo $BRANCH_DATA | jq -r '.[0].api_version'`
      if [[ "${BUILD_API[@]}"  =~ "${BRANCH_API}" ]]; then
        if [ $BRANCH_API == "api16" ];then
          export OHOS_NDK_HOME=""
        else
          export OHOS_NDK_HOME="${PREFIX}/sdk_api18/default/openharmony/native"
        fi
        list_config
      fi
    done
  fi
}

function list_flutter_version(){
  FLUTTER_VERSIONS=`cat $CONFIG_JSON | jq -r '.flutter_versions'`
  FLUTTER_VERSION_NUM=`echo $FLUTTER_VERSIONS | jq -r '.[0].flutter_version_num'`

  for ((INDEX=1; INDEX<=FLUTTER_VERSION_NUM; INDEX++))
  do
    VERSION_CONFIG=`echo $FLUTTER_VERSIONS | jq -r ".[0].version${INDEX}"`
    list_branch
  done
}

SCRIPT_DIR=$(cd $(dirname $0);pwd)
HOST_ARCH=""
PREFIX=""
get_host_arch
source $SCRIPT_DIR/arch/$HOST_ARCH/env.sh
#BUILD_FLUTTER_VERSION=("3.7" "3.22" "3.27")
BUILD_FLUTTER_VERSION=("3.7")
# BUILD_API=("api16" "api18" "api19")
BUILD_API=("api16")
CONFIG_JSON=$SCRIPT_DIR/arch/$HOST_ARCH/build_config.json
FLUTTER_VERSIONS=""
VERSION_CONFIG=""
BRANCH_CONFIG=""
BRANCH_DATA=""

list_flutter_version